<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>vue-resource</title>
    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <script src="../node_modules/vue/dist/vue.js"></script>
    <script src="../node_modules/axios/dist/axios.js"></script>
</head>

<body>
    <div id="app" class="container">
        <h2>axios 插件 get post http 全局拦截</h2>
        <a href="javascript:;" class="btn btn-primary" @click="get">Get请求</a>
        <a href="javascript:;" class="btn btn-primary" @click="post">Post请求</a>
        <!--<a href="javascript:;" class="btn btn-primary">JSONP请求</a>-->
        <a href="javascript:;" class="btn btn-primary" @click="http">http请求</a>
        </button>
        <br/>
        <span>{{msg}}</span>
    </div>
    <script>
        new Vue({
            el: '#app',
            data: {
                msg: '',
            },
            mounted: function() {
                axios.interceptors.request.use(function(config) {
                    console.log("request init");
                    return config;
                })
                axios.interceptors.response.use(function(response) {
                    console.log("response init.");
                    return response;
                })
            },
            methods: {
                get: function() {
                    axios.get("../package.json", {
                        params: {
                            userId: "Sky-Blue"
                        },
                        headers: {
                            token: "The get token"
                        }
                    }).then(res => {
                        this.msg = res.data;
                    }).catch(function(error) {
                        console.log("error init.")
                    })
                },
                post: function() {
                    axios.post("../package.json", {
                        userId: "Sky-Blue"
                    }, {
                        params: {
                            userId: "Sky-Blue"
                        },
                        headers: {
                            token: "The post token"
                        }
                    }, ).then(res => {
                        this.msg = res.data;
                    }).catch(function(error) {
                        console.log("error init.")
                    })
                },
                http: function() {
                    axios({
                        url: "../package.json",
                        method: "post",
                        data: {
                            userId: "Sky-Blue"
                        },
                        params: {
                            userId: "Sky-Blue"
                        },
                        headers: {
                            token: "The http token"
                        }
                    }).then(res => {
                        this.msg = res.data;
                    }).catch(function(error) {
                        console.log("error init.")
                    })
                }
            }
        })
    </script>
</body>

</html>
